package com.front.web.member;

import com.bootdo.common.utils.R;
import com.bootdo.lvyou.domain.XcxConfig;
import com.bootdo.lvyou.service.XcxConfigService;
import com.bootdo.updateApp.domain.AppUpdate;
import com.bootdo.updateApp.service.AppUpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/f/shequ/APPUpgradeController/")
public class APPUpgradeController {

    @Autowired
    private AppUpdateService appUpdateService;
    @RequestMapping("update")
    public R update(@RequestParam(value = "name",defaultValue = "")String name,@RequestParam(value = "version",defaultValue = "")String version ){
        HashMap<String, Object> param = new HashMap<>();
        param.put("sort","create_time");
        param.put("order","desc");
        List<AppUpdate> list = appUpdateService.list(param);
        if(list.size()==0||list==null){
            return R.ok().put("update","2");
        }
        AppUpdate appUpdate = list.get(0);//拿到第一个添加的

        String nameNew = appUpdate.getAppName();//APP名称
        String versionNew = appUpdate.getAppVersion();//APP版本号
        String androidUrl = appUpdate.getAndroidUrl();//Android更新地址
        String iosUrl = appUpdate.getIosUrl();//ios更新地址
        String[] versionNewPrefix = versionNew.split("\\.");
        String[] versionPrefix = version.split("\\.");
        if(!versionNewPrefix[0].equals(versionPrefix[0])){//如果版本号的 首位不一致 则是大版本更新
            return R.ok().put("wgtUrl",appUpdate.getWgtUrl()).put("appName",nameNew).put("appVersion",versionNew).put("update","0").put("androidUrl",androidUrl).put("iosUrl",iosUrl);
        }else{
            if(!version.equals(versionNew)){
                return R.ok().put("wgtUrl",appUpdate.getWgtUrl()).put("appName",nameNew).put("appVersion",versionNew).put("update","1").put("pkgUrl",appUpdate.getPkgUrl());
            }
        }
        return R.ok().put("update","2");
    }

}
